var categoryItems;
var categoryIdsArray = new Array();
var ajaxFinish = 0;
$().ready(function(){
	$.ajax({
		type: "POST",
		url: getProjectName()+"/topic/queryAllCategory",
		async: true,
		dataType:"json",
		success: function(json,status){
			categoryItems = json;
			if(json.success == true){
				ajaxFinish = 1;
			}
		},
		error : function(data, status, e){
			alert(alert(e+"\\获取分类信息失败,请刷新页面重试"));
		}
    });
	//发布主题帖
	$("#submitTopicButton").click(function(){
		var topicTitle = $("input[name=topicTitle]");//主题帖title
		var topicContent = $("textarea[name=topicContent]");//楼主说
		var productType = $("input[name=productType]:checked");//商品类型，1，2，3
		var productName = $("input[name=productName]");
		var categoryNumber = $("input[name=categoryNumber]");//分类number
		var productBrand = $("input[name=productBrand]");//商品品牌
		var productMarketPrice = $("input[name=productMarketPrice]");//商品市场价
		var productPicturePath = $("input[name=productPicturePath]");//商品主图
		var productDescriptionString = $("#productDescriptionString");//@商家string
		var topicPrice = $("input[name=topicPrice]");
		alert(productDescriptionString.html())
//		alert(topicDealerString.checkTopicDealerString());
		var _data = {'topicTitle':topicTitle.val(),'topicString':topicContent.val(),'productType':productType.val(),'categoryNumber':categoryNumber.val(),
							'productBrand':productBrand.val(),'productMarketPrice':productMarketPrice.val(),'productPicturePath':productPicturePath.val(),'productDescriptionString':productDescriptionString.html(),
							'productName':productName.val(),'topicPrice':topicPrice.val()
		}
		var f = $.checkTopicTitle(topicTitle) && $.checkTopicContent(topicContent) &&$.checkProductType(productType) && $.checkCategoryNumber(categoryNumber) && $.checkProductName(productName) && $.checkProductBrand(productBrand) && $.checkProductMarketPrice(productMarketPrice) &&  $.checkTopicPrice(topicPrice) && $.checkProductPicturePath(productPicturePath) && $.checkProductDescriptionString(productDescriptionString) ;
		alert(f);
		$father = $(this);
		$father.attr("disabled",true);
		$.ajax({
			type: "POST",
			url: getProjectName()+"/topic/submitTopic",
			data:_data,
			dataType:"json",
			beforeSend:function(){
				if(f == false){
					$father.attr('disabled',false);
				}
				return f;
			},
			success: function(json,status){
//				alert(json.redirectUrl)
				window.location.href=$.getProjectName()+json.redirectUrl;
			},
			error : function(data, status, e){
				alert(alert(e+"\\提交主题帖失败，请刷新重试"));
			}
	    });
	});
	$.topicTitleRegex = "^[\u4e00-\u9fa5a-zA-Z0-9_]{3,34}$";
	$.topicContentRegex = "^[\u4e00-\u9fa5a-zA-Z0-9_]{3,140}$"
	$.productTypeRegex = "^[1,2,3]{1}$"	
	$.categoryNumberRegex = "^\\d{7}$"
	$.	productBrandRegex = "^[\u4e00-\u9fa5a-zA-Z0-9_]{1,20}$";
	$.productMarketPriceRegex = "^\\d+(\.\\d+)?$";
	$.productDescriptionStringRegex = "^.{1,25000}$";//
	$.productNameRegex = "^.{2,40}$";
	$.productPicturePathRegex = "^(\/[A-Za-z0-9\._])+"
	$.checkTopicPrice = function(object){
		var re = new RegExp($.productMarketPriceRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">输入正确的价格</span>');
			 object.focus();
		 }
		 return f;
	} 	
	$.checkTopicTitle = function(object){
		 var re = new RegExp($.topicTitleRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">请输入34个字内标题</span>');
			 object.focus();
		 }
		 return f;
	};
	$.checkProductPicturePath = function(object){
		 var re = new RegExp($.productPicturePathRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">请选择商品主图</span>');
			 object.focus();
		 }
		 return f;
	};
	$.checkProductName= function(object){
		 var re = new RegExp($.productNameRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">请输入34个字内商品名称</span>');
			 object.focus();
		 }
		 return f;
	};
	$.checkTopicContent = function(object){
		 var re = new RegExp($.topicContentRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">输入140个字内</span>');
			 object.focus();
		 }
		 return f;
	};
	$.checkProductType = function(object){
		 var re = new RegExp($.productTypeRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">商品类型错误</span>');
			 object.focus();
		 }
		 return f;
	};
	$.checkCategoryNumber = function(object){
		 var re = new RegExp($.categoryNumberRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">请选择分类</span>');
			 object.focus();
		 }
		 return f;
	};
	$.checkProductBrand = function(object){
		 var re = new RegExp($.productBrandRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">输入20个字内</span>');
			 object.focus();
		 }
		 return f;
	};
	$.checkProductMarketPrice = function(object){
		 var re = new RegExp($.productMarketPriceRegex);
		 var f  =  re.test(object.val().toString());
		 if(f ==false){
			 object.after('<span style="color:red">输入正确的价格</span>');
			 object.focus();
		 }
		 return f;
	};
	$.checkProductDescriptionString = function(object){
		 var re = new RegExp($.productDescriptionStringRegex);
		 var f  =  re.test(object.html().toString());
		 if(f ==false){
			 object.after('<span style="color:red">输入25000个字内</span>');
			 object.focus();
		 }
		 return f;
	};
	
	$.getProjectName = function (){  
	    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp  
	    var curWwwPath=window.document.location.href;  
	    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp  
	    var pathName=window.document.location.pathname;  
	    var pos=curWwwPath.indexOf(pathName);  
	    //获取主机地址，如： http://localhost:8083  
	    var localhostPaht=curWwwPath.substring(0,pos);  
	    //获取带"/"的项目名，如：/uimcardprj  
	    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
	    return(projectName);  
	} 
	
	//alert('title:'+topicTitle.val()+'/content:'+topicContent.val()+'/type:'+productType.val()+'number:'+categoryNumber.val()+'/brand:'+productBrand.val()+'/price:'+productMarketPrice.val()+'/dealerString:'+topicDealerString.val()+'/path:'+productPicturePath.val())
	
	
});
//显示商品类型菜单
function showProductType(){
	var categoryType = $("input[name=productType]:checked").val();
	showFirstCategory(categoryType)
}
function showFirstCategory(categoryType){
	 categoryDiv = $("#categoryDiv");
	if(ajaxFinish == 1){
		var html="<div>请选择分类:</div><div id='firstCategory'><ul>";
		if(categoryType == 1 || categoryType ==3){//商品或者二手类型
			if(categoryType ==1){
				categoryName = "标准商品"
			}else{
				categoryName = "二手商品"
			}
			
			for(var i=0, l=categoryItems.content.length; i<l; i++){  
				//构造分类categoryItems JSON object
				if(categoryItems.content[i].level == 2 && categoryItems.content[i].categoryNumber < 2000000){
					html += '<li><a href="javascript:;" onclick="showSubCategoryById(\''+categoryItems.content[i].name+'\','+categoryItems.content[i].id+','+categoryItems.content[i].level+');">'+categoryItems.content[i].name+'</a></li>'
				}
			}
		}
		//1150000本地生活父类
		if(categoryType == 2){//本地生活服务
			categoryName = "本地生活服务"
			for(var i=0, l=categoryItems.content.length; i<l; i++){ 
				if(categoryItems.content[i].categoryNumber > 2000000 && categoryItems.content[i].level == 3 ){
					html += '<li><a href="javascript:;" onclick="showSubCategoryById(\''+categoryItems.content[i].name+'\','+categoryItems.content[i].id+','+categoryItems.content[i].level+');">'+categoryItems.content[i].name+'</a></li>'
				}
			}
		}
		categoryDiv.html(html+'</ul></div><div style="position: relative;bottom:0px"><input type="button" value="确定" /><input type="button" value="取消" onclick="toggleObj(\'categoryDiv\')"/></div>');
		
		categoryDiv.show();
	}else{
		alert("正在请求分类数据,请点击重试")
	}
}
//显示子分类category
function showSubCategoryById(fatherCategoryName,pid,level){
	if(categoryName != undefined){
		categoryName = categoryName+'>>'+fatherCategoryName;
	}else{
		categoryName = fatherCategoryName;
	}
		
//	alert(fatherCategoryName+pid+'/'+level);
	var html="<div>请选择分类:</div><div id='secondCategory'><ul>";
	for(var i=0, l=categoryItems.content.length; i<l; i++){ 
		if(categoryItems.content[i].pid == pid && categoryItems.content[i].level < 4){ //有下级分类
			html += '<li><a href="javascript:;" onclick="showSubCategoryById(\''+categoryItems.content[i].name+'\','+categoryItems.content[i].id+','+categoryItems.content[i].level+');">'+categoryItems.content[i].name+'</a></li>'
		}
		if(categoryItems.content[i].pid == pid && categoryItems.content[i].level == 4){ 
			html += '<li><a href="javascript:;" onclick="chooseCategoryNumber(\''+categoryItems.content[i].name+'\','+categoryItems.content[i].categoryNumber+');">'+categoryItems.content[i].name+'</a></li>'
		}
	}
	categoryDiv.html(html+'</ul></div><div style="position: relative;bottom:0px"><input type="button" value="上一级" /><input type="button" value="取消" onclick="toggleObj(\'categoryDiv\')"/></div>');
}


function chooseCategoryNumber(fatherCategoryName,categoryNumber){
	categoryName = categoryName + '>>'+fatherCategoryName;
//	alert(categoryName+categoryNumber);
	$("#categoryNumber").val(categoryNumber);
	categoryDiv.hide();
	$("#showNumber").html(categoryName)
}

	function ajaxFileUpload() {
		$("#loading").ajaxStart(function() {
			$(this).show();
		})//开始上传文件时显示一个图片
		.ajaxComplete(function() {
			$(this).hide();
		});//文件上传完成将图片隐藏起来
		$.ajaxFileUpload({
			url : 'common/picFileUpload.action',//用于文件上传的服务器端请求地址
			secureuri : false,//一般设置为false
			fileElementId : 'productPicture',//文件上传空间的id属性  <input type="file" id="file" name="file" />
			dataType : 'json',//返回值类型 一般设置为json
			success : function(data, status){ //服务器成功响应处理函数
				alert(data.message);//从服务器返回的json中取出message中的数据
				if (typeof (data.success) != 'undefined') {
					if (data.success != true) {
						alert(data.message);
					} else {
						$('#uploadPic').attr('src',getProjectName()+"/"+data.picture_path);
						$('#uploadPic').show();
						$('#productPicturePath').val(data.picture_path);
						//alert(data.picture_path);
					}
				}
			},
			error : function(data, status, e){//服务器响应失败处理函数
				alert(e);
			}
		});
		return false;
	}
function toggleObj(objN){
		$("#"+objN).hide();
}
function getProjectName(){  
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp  
    var curWwwPath=window.document.location.href;  
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp  
    var pathName=window.document.location.pathname;  
    var pos=curWwwPath.indexOf(pathName);  
    //获取主机地址，如： http://localhost:8083  
    var localhostPaht=curWwwPath.substring(0,pos);  
    //获取带"/"的项目名，如：/uimcardprj  
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
    return(projectName);  
} 

function checkTile(){
	
	
}
function isURL(str) {
   var strRegex = "^[a-z0-9A-Z]{3}\.[a-z0-9A-Z][a-z0-9A-Z]{0,61}?[a-z0-9A-Z]\.com|net|cn|cc (:s[0-9]{1-4})?/$";
   var re = new RegExp(strRegex);
   return re.test(str);
}

